package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.Model;
import model.UserDAO;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import formbeans.LoginForm;

import beans.User;

/*
 *  Course: S09-15437 - WEB APPLICATION DEVELOPMENT 
 * 	Name: Jiale Huang
 * 	Andrew ID: jialeh
 * 	Date: 2/23/2009
 *  Assignment: Hw4
 */


public class LoginAction extends Action{

	private FormBeanFactory<LoginForm> formBeanFactory = FormBeanFactory.getInstance(LoginForm.class);
	
	private UserDAO userDAO;
	
	public LoginAction(Model model){
		
		userDAO = model.getUserDAO();
	}
	
	@Override
	public String getName() {

		return "login.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);		
		
		try{
			
			LoginForm form = formBeanFactory.create(request);
			
   		    // If the servlet path isn't "/login.do", the controller sent a request here
	        // because the user needed to login.  We want to support redirect back to the
	        // original request only if the user is just trying to view an image.
	        // So, only set up redirect back to original request path="/view.do".
	        /*if (request.getServletPath().equals("/view.do")) {
	        	String redirectTo = request.getRequestURI()+"?"+request.getQueryString();
	        	HttpSession session = request.getSession();
	        	session.setAttribute("redirectTo",redirectTo);
	        }*/
		
			if(!form.isPresent()){
				
				return "login.jsp";
			}
			
	        // Any validation errors?
	        errors.addAll(form.getValidationErrors());
	        if (errors.size() != 0) {
	            return "login.jsp";
	        }
	        
	        // Look up the user
	        User user = userDAO.lookup(form.getUserEmail());
			
	        if (user == null) {
	            errors.add("User Name not found");
	            return "login.jsp";
	        }
	        
	        // Check the password
	        if (!user.checkPassword(form.getPassword())) {
	            errors.add("Incorrect password");
	            return "login.jsp";
	        }
	        
	        // Attach (this copy of) the user bean to the session
	        HttpSession session = request.getSession();
	        session.setAttribute("user",user);
	        
	        // After successful login send to...
	        String redirectTo = (String) session.getAttribute("redirectTo");
	        if (redirectTo != null) return redirectTo;
	        
	        // If redirectTo is null, redirect to the "manage" action
			String webapp = request.getContextPath();
			return webapp + "/manage.do";
						
		} catch(DAOException e){
        	errors.add(e.getMessage());
        	return "login.jsp";			
		} catch(FormBeanException e){
        	errors.add(e.getMessage());
        	return "login.jsp";
		}
	}

}
